<!DOCTYPE html>
<meta charset='utf-8'>
<title>Window Placement: getScreensDeprecated() tentative</title>
<!-- TODO: update link to W3C whenever specifications are ready -->
<link rel='help' href='https://github.com/webscreens/window-placement'/>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='resources/screenenumeration-helpers.js'></script>
<script>

'use strict';

function check_screen_matches_display(screen, display) {
  assert_equals(screen.left, display.bounds.x);
  assert_equals(screen.top, display.bounds.y);
  assert_equals(screen.width, display.bounds.width);
  assert_equals(screen.height, display.bounds.height);
  assert_equals(screen.availLeft, display.workArea.x);
  assert_equals(screen.availTop, display.workArea.y);
  assert_equals(screen.availWidth, display.workArea.width);
  assert_equals(screen.availHeight, display.workArea.height);
  assert_equals(screen.scaleFactor, display.deviceScaleFactor);
}

screen_enumeration_test(async (t, mockScreenEnum) => {
  mockScreenEnum.setSuccess(true);
  await test_driver.set_permission({name: 'window-placement'}, 'granted');
  assert_equals((await self.getScreensDeprecated()).length, 0);
}, 'getScreensDeprecated() supports an empty set of mocked screens');

screen_enumeration_test(async (t, mockScreenEnum) => {
  let display1 = makeDisplay(10,
                             {x: 0, y: 0, width: 800, height: 600},
                             {x: 0, y: 0, width: 800, height: 550},
                             1.0);

  mockScreenEnum.addDisplay(display1);
  mockScreenEnum.setPrimaryId(mockScreenEnum.displays_[0].id);
  mockScreenEnum.setInternalId(mockScreenEnum.displays_[0].id);
  mockScreenEnum.setSuccess(true);

  await test_driver.set_permission({name: 'window-placement'}, 'granted');

  const screens = await self.getScreensDeprecated();
  assert_equals(screens.length, 1);
  check_screen_matches_display(screens[0], display1);
  assert_equals(screens[0].primary, true);
  assert_equals(screens[0].internal, true);
  assert_equals(screens[0].id, '0');
}, 'getScreensDeprecated() supports a single mocked screen');

screen_enumeration_test(async (t, mockScreenEnum) => {
  let display1 = makeDisplay(10,
                             {x: 0, y: 0, width: 800, height: 600},
                             {x: 0, y: 0, width: 800, height: 550},
                             1.0);

  let display2 = makeDisplay(11,
                             {x: 800, y: 0, width: 800, height: 600},
                             {x: 800, y: 0, width: 800, height: 550},
                             2.0);

  let display3 = makeDisplay(12,
                             {x: 0, y: 600, width: 1200, height: 800},
                             {x: 50, y: 50, width: 1150, height: 750},
                             1.5);

  mockScreenEnum.addDisplay(display1);
  mockScreenEnum.addDisplay(display2);
  mockScreenEnum.addDisplay(display3);
  mockScreenEnum.setPrimaryId(mockScreenEnum.displays_[0].id);
  mockScreenEnum.setInternalId(mockScreenEnum.displays_[0].id);
  mockScreenEnum.setSuccess(true);

  await test_driver.set_permission({name: 'window-placement'}, 'granted');

  let screens = await self.getScreensDeprecated();
  assert_equals(screens.length, 3);
  check_screen_matches_display(screens[0], display1);
  assert_equals(screens[0].primary, true);
  assert_equals(screens[0].internal, true);
  assert_equals(screens[0].id, '0');
  check_screen_matches_display(screens[1], display2);
  assert_equals(screens[1].primary, false);
  assert_equals(screens[1].internal, false);
  assert_equals(screens[1].id, '1');
  check_screen_matches_display(screens[2], display3);
  assert_equals(screens[2].primary, false);
  assert_equals(screens[2].internal, false);
  assert_equals(screens[2].id, '2');

  mockScreenEnum.removeDisplay(display2.id);
  screens = await self.getScreensDeprecated();
  assert_equals(screens.length, 2);
  check_screen_matches_display(screens[0], display1);
  assert_equals(screens[0].id, '0');
  check_screen_matches_display(screens[1], display3);
  assert_equals(screens[1].id, '1');

  mockScreenEnum.removeDisplay(display1.id);
  screens = await self.getScreensDeprecated();
  assert_equals(screens.length, 1);
  check_screen_matches_display(screens[0], display3);
  assert_equals(screens[0].id, '0');
}, 'getScreensDeprecated() supports multiple mocked screens');

screen_enumeration_test(async (t, mockScreenEnum) => {
  mockScreenEnum.setSuccess(false);
  await test_driver.set_permission({name: 'window-placement'}, 'granted');
  promise_rejects_dom(t, 'NotAllowedError', self.getScreensDeprecated());
}, 'getScreensDeprecated() rejects when the mock success value is set false');

</script>
